﻿
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using MyDb.DoMain.DbContext;
using MyDb.DoMain.Entity.DbEntity;
using MyDb.DoMain.Interface;

namespace MyDb.DoMain.Service
{
    public class BaseDataService : IBaseDataService
    {

        /// <summary>
        /// 根据角色id字符串获取对应的菜单权限
        /// </summary>
        /// <param name="roleIds">角色id字符串</param>
        public List<T_Module> GetModuleList(string roleIds)
        {
            using (var dbContext = new MyDbContext())
            {
                var roleIdStrList = roleIds.Split(',').ToList();
                var roleIdList = new List<int>();
                foreach (var roleIdStr in roleIdStrList)//在这里转化成int类型而不是在where里面使用tostring方法是防止报错
                {
                    var roleId = 0;
                    if (int.TryParse(roleIdStr, out roleId))
                        roleIdList.Add(roleId);
                }
                //获取菜单id
                var menuIds = dbContext.RoleModules.Where(rm => roleIdList.Contains(rm.RoleId)).Select(m => m.ModuleId);
                var menuList = dbContext.Modules.Where(m => menuIds.Contains(m.Id)).ToList();
                return menuList;
            }
        }
    }
}
